package cn.csc.module.shoppingCard.controller.admin.points;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.shoppingCard.controller.admin.points.vo.*;
import cn.csc.module.shoppingCard.convert.points.CardPointsConvert;
import cn.csc.module.shoppingCard.dal.dataobject.points.CardPointsDO;
import cn.csc.module.shoppingCard.service.points.CardPointsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 购物卡积分")
@RestController
@RequestMapping("/shoppingCard/card-points")
@Validated
public class CardPointsController {

    @Resource
    private CardPointsService cardPointsService;

    @PostMapping("/create")
    @ApiOperation("创建购物卡积分")
    @PreAuthorize("@ss.hasPermission('shoppingCard:card-points:create')")
    public CommonResult<BigInteger> createCardPoints(@Valid @RequestBody CardPointsCreateReqVO createReqVO) {
        return success(cardPointsService.createCardPoints(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新购物卡积分")
    @PreAuthorize("@ss.hasPermission('shoppingCard:card-points:update')")
    public CommonResult<Boolean> updateCardPoints(@Valid @RequestBody CardPointsUpdateReqVO updateReqVO) {
        cardPointsService.updateCardPoints(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除购物卡积分")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shoppingCard:card-points:delete')")
    public CommonResult<Boolean> deleteCardPoints(@RequestParam("id") BigInteger id) {
        cardPointsService.deleteCardPoints(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得购物卡积分")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shoppingCard:card-points:query')")
    public CommonResult<CardPointsRespVO> getCardPoints(@RequestParam("id") BigInteger id) {
        CardPointsDO cardPoints = cardPointsService.getCardPoints(id);
        return success(CardPointsConvert.INSTANCE.convert(cardPoints));
    }

    @GetMapping("/list")
    @ApiOperation("获得购物卡积分列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('shoppingCard:card-points:query')")
    public CommonResult<List<CardPointsRespVO>> getCardPointsList(@RequestParam("ids") List<BigInteger> ids) {
        List<CardPointsDO> list = cardPointsService.getCardPointsList(ids);
        return success(CardPointsConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得购物卡积分分页")
    @PreAuthorize("@ss.hasPermission('shoppingCard:card-points:query')")
    public CommonResult<PageResult<CardPointsRespVO>> getCardPointsPage(@Valid CardPointsPageReqVO pageVO) {
        PageResult<CardPointsDO> pageResult = cardPointsService.getCardPointsPage(pageVO);
        return success(CardPointsConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出购物卡积分 Excel")
    @PreAuthorize("@ss.hasPermission('shoppingCard:card-points:export')")
    @OperateLog(type = EXPORT)
    public void exportCardPointsExcel(@Valid CardPointsExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CardPointsDO> list = cardPointsService.getCardPointsList(exportReqVO);
        // 导出 Excel
        List<CardPointsExcelVO> datas = CardPointsConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "购物卡积分.xls", "数据", CardPointsExcelVO.class, datas);
    }

}
